// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

 
@ccclass
export default class UIController extends cc.Component {

    static UiPrefabPath={
        GameOver:"gameover_panel",
        Teach:"teach_panel",
        Ranking:'ranking_panel'
    }

    nodes=[];

    isLoadingPrefab=false;
    // LIFE-CYCLE CALLBACKS:
    private static _inst:UIController;

    @property(cc.Node)
    maskNode:cc.Node=null;

    static getInst(){
        return UIController._inst;
    }

     onLoad () {
        UIController._inst=this;
      }

    // start () {

    // }
    


    show(name){
      this.checkIsInit(name,()=>{
        this.nodes[name].active=true;
      });
    }
    showWithMask(name){
        this.checkIsInit(name,(exists)=>{
            if(!exists){
                this.maskNode.parent=this.nodes[name];
                this.maskNode.zIndex=-1;
                this.maskNode.active=true;
            }
            this.nodes[name].active=true;
        });
    }


    checkIsInit(name,cb){
        if(!this.nodes[name]){
            if(!this.isLoadingPrefab){
                this.isLoadingPrefab=true;
                cc.loader.loadRes("prefabs/"+name,  (err, prefab)=> {
                    this.nodes[name]=cc.instantiate(prefab);
                    this.node.addChild(this.nodes[name]);
                    this.isLoadingPrefab=false;
                     if(err){
                         console.error(err);
                     }
                     else{
                        cb(false);
                     }
                });
            }
        }
        else{
            cb(true);
        }
    }

    // update (dt) {}
}
